home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr25 / wordy.zip / XFIND.C < prev    next >
C/C++ Source or Header  |  1995-04-29  |  6KB  |  227 lines

  1. /**************************************************************************/
  2. /*                              XFIND UTILITY                          */
  3. /*                                                            */
  4. /*                                 M\Cooper                            */
  5. /*                        3425 Chestnut Ridge Rd.                        */
  6. /*                        Grantsville, MD 21536-9801                    */
  7. /*                        --------------------------                    */
  8. /*                        Email:  thegrendel@aol.com                    */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package             */
  11. /*                                                            */    
  12. /**************************************************************************/
  13.  
  14. #include <conio.h>
  15. #include "srch.h"
  16. //srch.h (source) is part of the WORDY package
  17.  
  18.  
  19. #define FILE_OPENING_ERROR 3
  20. #define FILENAME_MAXLEN 8
  21. #define CR "\n"
  22. #define FILE_SUFFIX ".fnd"
  23. #define MAXLEN 30
  24. #define LINE_LEN 80
  25. #define NOARGS 1
  26. #define INCREMENT 1
  27. #define SPACE ' '
  28. #define DBLBAR 205
  29. #define WILDCARD '?'
  30. //Wildcard character in command line, may be changed
  31.  
  32. #define BUFFERSIZE 8192
  33.  
  34. char ad[] =
  35. "XFIND utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536-9801";
  36.  
  37.  
  38. void getword( char *lset );
  39. void center( char *strng );
  40.  
  41. typedef enum { FALSE, TRUE } Boolean;
  42.  
  43. void main( int argc, char **argv )
  44. {
  45.  
  46.    char letterset[ MAXLEN ];
  47.  
  48.      if( argc == NOARGS )
  49.         {
  50.         clrscr();
  51.         puts( "Enter a LETTER PATTERN to test [example ab?c??e... " );
  52.         gets( letterset );
  53.         }
  54.      else
  55.         strcpy( letterset, *( argv + 1 ) );
  56.  
  57.      getword( letterset );
  58. }
  59.  
  60.  
  61. /**********************************WORDTEST********************************/
  62. /*       Function tests if word is constructible from Letterset            */
  63. /*                 Args in: char *letterset, char *word                          */
  64. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  65. /**************************************************************************/
  66.  
  67. Boolean wordtest( char *letterset, char *word )
  68. {
  69.     Boolean error_flag;
  70.     static char dup_lset[ MAXLEN ];
  71.     register unsigned int cnt = 0,
  72.                       u,
  73.                       v;
  74.  
  75.      strcpy( dup_lset, letterset );
  76.          
  77.      u = strlen( word );
  78.       v = strlen( letterset );
  79.  
  80.      while( ( *letterset == *word ) || *letterset == WILDCARD )
  81.         {
  82.         letterset++;
  83.         word++;
  84.         cnt++;
  85.         }
  86.  
  87.      if( u <= cnt && u == v )
  88.         error_flag = TRUE;
  89.      else
  90.         error_flag = FALSE;
  91.  
  92.  
  93.         return( error_flag );
  94. }
  95.  
  96. /**************************************************************************/
  97.  
  98. void getword( char *letter_set )
  99. {
  100.  
  101.     char    l_set [ MAXLEN ],
  102.         word [ MAXLEN ],
  103.         tempstr [ MAXLEN + 1 ],
  104.         targetfile [ MAXLEN ],
  105.         bar [ LINE_LEN + 1 ],
  106.         double_bar [ LINE_LEN + 1 ],
  107.         messg [7];
  108.  
  109.     FILE *fptr,
  110.         *tfile;
  111.     int fnamelen;
  112.     long wcount = 0L;
  113.  
  114.        memset( bar, '*', LINE_LEN );
  115.        *( bar + LINE_LEN ) = NULL;
  116.        memset( double_bar, DBLBAR, LINE_LEN );
  117.        *( double_bar + LINE_LEN ) = NULL;
  118.  
  119.        /*************opening credits*************/
  120.        clrscr();
  121.        printf( double_bar );
  122.        strcpy( tempstr, ad );
  123.        center ( tempstr );
  124.        printf( tempstr );
  125.        printf( CR );
  126.        printf( double_bar );
  127.        printf( CR );
  128.        /****************************************/
  129.  
  130.  
  131.        strcpy ( l_set, letter_set );
  132.        strcat ( letter_set, CR );
  133.  
  134.        /*   Create name of file to store derived words in   */
  135.        /*********************************************************/
  136.        strcpy( targetfile, "matched.wds" );
  137.        /*********************************************************/
  138.  
  139.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  140.          {
  141.          printf( "\7\7\7Cannot open Wordfile!" );
  142.          exit( FILE_OPENING_ERROR );
  143.          }
  144.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  145.          exit( FILE_OPENING_ERROR + 1 );
  146.  
  147.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  148.          {
  149.          printf( "\7\7\7Cannot open file to save words in!" );
  150.          exit ( FILE_OPENING_ERROR + 1 );
  151.          }
  152.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  153.          exit( FILE_OPENING_ERROR + 2 );
  154.  
  155.        /**************'Wait' Message************/
  156.        printf( CR CR );
  157.        printf( "WORKING...\n\n" );
  158.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  159.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  160.        printf( "Now searching 99,000+ word file and writing file of valid words.\n\n" );
  161.        /*****************************************/
  162.  
  163.  
  164.  
  165.  
  166.  
  167.        sprintf( tempstr, "Words fitting the pattern of: %s\n", strupr( l_set ) );
  168.        center( tempstr );
  169.        fprintf( tfile, double_bar );
  170.       fprintf( tfile, CR );
  171.        fprintf( tfile, tempstr );
  172.        fprintf( tfile, double_bar );
  173.        fprintf( tfile, CR );
  174.  
  175.  
  176.          /*********************Main Loop*************/     
  177.           while( fgets( word, MAXLEN, fptr ) != NULL )
  178.  
  179.             if( wordtest( letter_set, word ) )
  180.                {
  181.                fprintf( tfile, "%s", word );
  182.                wcount++;
  183.                }
  184.           /*******************************************/
  185.  
  186.           fprintf( tfile, bar );
  187.       fprintf( tfile, CR );
  188.           sprintf( tempstr, "%ld words fit the pattern of %s.",
  189.                  wcount, l_set );
  190.           center( tempstr );              
  191.           fprintf( tfile, tempstr );
  192.           fprintf( tfile, "\n\n" );
  193.  
  194.           center( ad );
  195.           fprintf( tfile, ad );
  196.  
  197.           fcloseall();
  198.  
  199.           if( wcount == INCREMENT )
  200.             strcpy( messg, "word" );
  201.           else
  202.             strcpy( messg, "words" );
  203.           sprintf( tempstr,
  204.                  "The file %s has %ld %s fitting the pattern of %s\7.",
  205.                  targetfile, wcount, messg, l_set );
  206.           center( tempstr );
  207.           printf( CR CR );
  208.           printf( tempstr );
  209.  
  210. }
  211.  
  212.  
  213.  
  214. void center( char *str )
  215. {
  216.    int padding;
  217.    char st [ LINE_LEN + INCREMENT ];
  218.  
  219.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  220.      memset( st, SPACE, padding );
  221.      *( st + padding ) = NULL;  //Terminate string
  222.      strcat( st, str );
  223.      strcpy( str, st );
  224.  
  225.      return;
  226. }
  227.